package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.TrainingGoalDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.TrainingGoalEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.TrainingGoalItemVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
public class TrainingGoalManager extends BaseManager {
    private final TrainingGoalDAO trainingGoalDAO;

    public TrainingGoalManager(TrainingGoalDAO trainingGoalDAO) {
        this.trainingGoalDAO = trainingGoalDAO;

    }

    public Integer getPageCount(String name) {
        int count = trainingGoalDAO.count(name);
        return calcPageCount(count, TrainingGoalDAO.PAGE_SIZE);
    }

    @Transactional
    public List<TrainingGoalItemVO> getPage(Integer index, String namePart) {
        List<TrainingGoalItemVO> trainingGoalItemList = new ArrayList<>();
        List<TrainingGoalEntity> trainingGoalList = trainingGoalDAO.getPage(index, namePart);

        for (TrainingGoalEntity trainingGoal : trainingGoalList) {
            int id = trainingGoal.getId();
            String name = trainingGoal.getName();
            String remarks = trainingGoal.getRemarks();
            trainingGoalItemList.add(new TrainingGoalItemVO(id, name,remarks));
        }

        return trainingGoalItemList;
    }

    public TrainingGoalEntity get(Integer id) {
        return trainingGoalDAO.get(id);
    }

    public int create(TrainingGoalEntity entity) {
        return trainingGoalDAO.insert(entity);
    }

    public int update(TrainingGoalEntity entity) {
        return trainingGoalDAO.update(entity);
    }

    public int delete(Integer id) {
        return trainingGoalDAO.delete(id);
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<TrainingGoalEntity> entityList = trainingGoalDAO.listName();
        for (TrainingGoalEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
